﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SQLExecute;


public partial class profiles_list_profiles : System.Web.UI.Page
{
    private DataSet ds;
    private DataView dv = new DataView();
    private int salfrom = 0, salto = 0;
    static Guid id;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Init_Combo();
            SeperateCharAndNumber();
            BindGrid();
            AddOnClick();
        }
    }
    private void Init_Combo()
    {
        Common c = new Common();
        ds = c.Select_Common();
        cbSchool.DataSource = ds.Tables[4];
        cbSchool.DataTextField = "school_id";
        cbSchool.DataValueField = "school_id";
        cbSchool.DataBind();
        cbSchool.Items.Insert(0, new ListItem("ALL", "ALL"));
        cbSchool.SelectedIndex = 0;

        cbMajorSubject.DataSource = ds.Tables[3];
        cbMajorSubject.DataTextField = "majorsubject_name";
        cbMajorSubject.DataValueField = "id";
        cbMajorSubject.DataBind();
        cbMajorSubject.Items.Insert(0, new ListItem("ALL", "ALL"));
        cbMajorSubject.SelectedIndex = 0;       

        cbKnownFrom.DataSource = ds.Tables[0];
        cbKnownFrom.DataTextField = "known_from";
        cbKnownFrom.DataValueField = "id";
        cbKnownFrom.DataBind();
        cbKnownFrom.Items.Insert(0, new ListItem("ALL", "-1"));
        cbKnownFrom.SelectedIndex = 0;

        int j = 0;
        for (int i = 200; i < 900; i = i + 100)
        {
            j = i + 100;
            cbExpectedSalaryFrom.Items.Add(i.ToString() + " to " + j.ToString());
            //cbExpectedSalaryTo.Items.Add(i.ToString());
        }
        cbExpectedSalaryFrom.Items.Insert(0, new ListItem("ALL", "-1"));
        cbExpectedSalaryFrom.SelectedIndex = 0;


    }
    private void AddOnClick()
    {
        txtFullName.Attributes.Add("onKeyPress", "KeyDownHandler()");

    }
    private void SeperateCharAndNumber()
    {
        if (cbExpectedSalaryFrom.SelectedValue != "-1")
        {

            string s = cbExpectedSalaryFrom.Text;
            //sal
            string strSalFrom = s.Substring(0, 3);
            salfrom = Convert.ToInt16(strSalFrom);
            string strSalTo = s.Substring(7, 3);
            salto = Convert.ToInt16(strSalTo);
        }
        else
        {
            salfrom = 0;
            salto = 0;
        }

    }
    private void ClearNumberZero(DataSet ds)
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["expectedsalary"].ToString() == "0")
            {
                ds.Tables[0].Rows[i]["expectedsalary"] = DBNull.Value;
                ds.Tables[0].Rows[i]["currency_salary"] = DBNull.Value;
            }
        }
    }
    private void BindGrid()
    {
        SeperateCharAndNumber();

        Profiles p = new Profiles();

        ds = p.Get_List_Profiles(cbSchool.SelectedValue, cbMajorSubject.SelectedValue, salfrom, salto, Convert.ToInt16(cbKnownFrom.SelectedValue));
        ClearNumberZero(ds);
        //dv = new DataView(ds.Tables[0]);
        //dv.Sort = sortExpression + direction;


        if (ViewState["sortExpr"] != null)
        {
            dv = new DataView(ds.Tables[0]);
            // dv.Sort = (string)ViewState["sortExpr"] + " " + sortingOrder;
            dv.Sort = (string)ViewState["sortExpr"];
            gdProfiles.DataSource = dv;
            gdProfiles.DataBind();
        }
        else
        {
            dv = ds.Tables[0].DefaultView;
            //ViewState["sortExpr"] = "asc";
            gdProfiles.DataSource = dv;
            gdProfiles.DataBind();
        }
        //gdProfiles.DataSource = ds;
        //gdProfiles.DataBind();


        gdProfiles.Columns[1].Visible = false;
        if (ds.Tables[0].Rows.Count > 0)
            ltTotalItems.Text = ds.Tables[0].Rows.Count.ToString();
        else ltTotalItems.Text = "0";

    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void gdProfiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_Profile(id);
    }
    protected void gdProfiles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkButtonDelete");
            l.Attributes.Add("onclick", "javascript:return "
                            + "confirm('Are you sure you want to delete this record: " + DataBinder.Eval(e.Row.DataItem, "fullname") + "')");
        }
    }
    private void Del_Profile(Guid id)
    {


        if (id != null)
        {
            SqlHelper.ExecuteNonQuery(Common.strConnect, "Del_Profile", new object[] { id });

        }

    }
    protected void gdProfiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            id = new Guid(e.CommandArgument.ToString());
            Del_Profile(id);
            BindGrid();
        }
    }
    protected void gdProfiles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdProfiles.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void gdProfiles_Sorting(object sender, GridViewSortEventArgs e)
    {

        //ViewState["sortExpr"] = e.SortExpression+ " " +sortingOrder;
        //if (e.SortExpression.
        ViewState["sortExpr"] = e.SortExpression;
        //ViewState["sortExpr"] = e.SortExpression + " " + sortingOrder;
        BindGrid();



    }
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortExpr"] == null)


                ViewState["sortExpr"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortExpr"];
        }

        set
        {
            ViewState["sortExpr"] = value;
        }

    }
    public string sortingOrder
    {

        get
        {
            if (ViewState["sortExpr"].ToString() == "desc")
                ViewState["sortExpr"] = "asc";
            else ViewState["sortExpr"] = "desc";

            return ViewState["sortExpr"].ToString();

        }

        set { ViewState["sortExpr"] = value; }
    }
    protected void gdProfiles_RowCreated(object sender, GridViewRowEventArgs e)
    {


        if (e.Row.RowType != DataControlRowType.Header)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#eaeaea'");
            e.Row.Attributes.Add("onmouseout", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ffffff'");
        }

    }
    private void BindGridFromSearch()
    {
        SeperateCharAndNumber();

        Profiles p = new Profiles();
        ds = SqlHelper.ExecuteDataset(Common.strConnect, "Search_Profiles_With_FullName", new object[] { txtFullName.Text });
        //ds = p.Get_List_Profiles(cbSchool.SelectedValue, cbMajorSubject.SelectedValue, Convert.ToInt16(cbDistrict.SelectedValue), salfrom, salto, Convert.ToInt16(cbKnownFrom.SelectedValue));

        //dv = new DataView(ds.Tables[0]);
        //dv.Sort = sortExpression + direction;


        if (ViewState["sortExpr"] != null)
        {
            dv = new DataView(ds.Tables[0]);
            // dv.Sort = (string)ViewState["sortExpr"] + " " + sortingOrder;
            dv.Sort = (string)ViewState["sortExpr"];
            gdProfiles.DataSource = dv;
            gdProfiles.DataBind();
        }
        else
        {
            dv = ds.Tables[0].DefaultView;
            //ViewState["sortExpr"] = "asc";
            gdProfiles.DataSource = dv;
            gdProfiles.DataBind();
        }
        //gdProfiles.DataSource = ds;
        //gdProfiles.DataBind();


        gdProfiles.Columns[1].Visible = false;

        if (ds.Tables[0].Rows.Count > 0)
            ltTotalItems.Text = ds.Tables[0].Rows.Count.ToString();
        else ltTotalItems.Text = "0";

    }
    private void Search_Profiles()
    {

        BindGridFromSearch();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtFullName.Text != "")
            Search_Profiles();
        else

            BindGrid();
    }
    protected void cbSchool_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void cbKnownFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
}
